{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15120"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import random\n",
    "import pathlib\n",
    "data_path = pathlib.Path('./assets')\n",
    "all_image_paths = list(data_path.glob('*/*'))  \n",
    "all_image_paths = [str(path) for path in all_image_paths]  # 所有图片路径的列表\n",
    "random.shuffle(all_image_paths)  # 打散\n",
    "\n",
    "image_count = len(all_image_paths)\n",
    "image_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['assets\\\\Kindred\\\\269.jpg',\n",
       " 'assets\\\\Zyra\\\\51.jpg',\n",
       " 'assets\\\\Thresh\\\\2.jpg',\n",
       " 'assets\\\\Leona\\\\175.jpg',\n",
       " 'assets\\\\Ashe\\\\194.jpg']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_image_paths[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Aatrox',\n",
       " 'Amumu',\n",
       " 'Annie',\n",
       " 'Ashe',\n",
       " 'Azir',\n",
       " 'Brand',\n",
       " 'Braum',\n",
       " 'Diana',\n",
       " 'DrMundo',\n",
       " 'Ezreal',\n",
       " 'Ivern',\n",
       " 'Janna',\n",
       " 'Jax',\n",
       " 'Karma',\n",
       " 'KhaZix',\n",
       " 'Kindred',\n",
       " 'KogMaw',\n",
       " 'LeBlanc',\n",
       " 'Leona',\n",
       " 'Lucian',\n",
       " 'Lux',\n",
       " 'Malphite',\n",
       " 'Malzahar',\n",
       " 'Maokai',\n",
       " 'MasterYi',\n",
       " 'Nami',\n",
       " 'Nasus',\n",
       " 'Nautilus',\n",
       " 'Neeko',\n",
       " 'Nocturne',\n",
       " 'Olaf',\n",
       " 'Ornn',\n",
       " 'Qiyana',\n",
       " 'RekSai',\n",
       " 'Renekton',\n",
       " 'Senna',\n",
       " 'Singed',\n",
       " 'Sion',\n",
       " 'Sivir',\n",
       " 'Skarner',\n",
       " 'Soraka',\n",
       " 'Syndra',\n",
       " 'Taliyah',\n",
       " 'Taric',\n",
       " 'Thresh',\n",
       " 'Twitch',\n",
       " 'Varus',\n",
       " 'Vayne',\n",
       " 'Veigar',\n",
       " 'Vladimir',\n",
       " 'Volibear',\n",
       " 'Warwick',\n",
       " 'Yasuo',\n",
       " 'Yorick',\n",
       " 'Zed',\n",
       " 'Zyra']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_names = sorted(item.name for item in data_path.glob('*/') if item.is_dir())\n",
    "label_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Aatrox': 0,\n",
       " 'Amumu': 1,\n",
       " 'Annie': 2,\n",
       " 'Ashe': 3,\n",
       " 'Azir': 4,\n",
       " 'Brand': 5,\n",
       " 'Braum': 6,\n",
       " 'Diana': 7,\n",
       " 'DrMundo': 8,\n",
       " 'Ezreal': 9,\n",
       " 'Ivern': 10,\n",
       " 'Janna': 11,\n",
       " 'Jax': 12,\n",
       " 'Karma': 13,\n",
       " 'KhaZix': 14,\n",
       " 'Kindred': 15,\n",
       " 'KogMaw': 16,\n",
       " 'LeBlanc': 17,\n",
       " 'Leona': 18,\n",
       " 'Lucian': 19,\n",
       " 'Lux': 20,\n",
       " 'Malphite': 21,\n",
       " 'Malzahar': 22,\n",
       " 'Maokai': 23,\n",
       " 'MasterYi': 24,\n",
       " 'Nami': 25,\n",
       " 'Nasus': 26,\n",
       " 'Nautilus': 27,\n",
       " 'Neeko': 28,\n",
       " 'Nocturne': 29,\n",
       " 'Olaf': 30,\n",
       " 'Ornn': 31,\n",
       " 'Qiyana': 32,\n",
       " 'RekSai': 33,\n",
       " 'Renekton': 34,\n",
       " 'Senna': 35,\n",
       " 'Singed': 36,\n",
       " 'Sion': 37,\n",
       " 'Sivir': 38,\n",
       " 'Skarner': 39,\n",
       " 'Soraka': 40,\n",
       " 'Syndra': 41,\n",
       " 'Taliyah': 42,\n",
       " 'Taric': 43,\n",
       " 'Thresh': 44,\n",
       " 'Twitch': 45,\n",
       " 'Varus': 46,\n",
       " 'Vayne': 47,\n",
       " 'Veigar': 48,\n",
       " 'Vladimir': 49,\n",
       " 'Volibear': 50,\n",
       " 'Warwick': 51,\n",
       " 'Yasuo': 52,\n",
       " 'Yorick': 53,\n",
       " 'Zed': 54,\n",
       " 'Zyra': 55}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_to_index = dict((name, index) for index, name in enumerate(label_names))\n",
    "label_to_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_image_labels = [label_to_index[pathlib.Path(path).parent.name] for path in all_image_paths]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assets\\Kindred\\269.jpg  --->   15\n",
      "assets\\Zyra\\51.jpg  --->   55\n",
      "assets\\Thresh\\2.jpg  --->   44\n",
      "assets\\Leona\\175.jpg  --->   18\n",
      "assets\\Ashe\\194.jpg  --->   3\n"
     ]
    }
   ],
   "source": [
    "for image, label in zip(all_image_paths[:5], all_image_labels[:5]):\n",
    "    print(image, ' --->  ', label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = tf.data.Dataset.from_tensor_slices((all_image_paths, all_image_labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_and_preprocess_from_path_label(path, label):\n",
    "    # read the img through file path\n",
    "    image = tf.io.read_file(path)  \n",
    "    image = tf.image.decode_jpeg(image, channels=3)\n",
    "    # 原始图片大小为(128, 128, 3)，重设为(64, 64)\n",
    "    image = tf.image.resize(image, [64, 64])  \n",
    "    image = tf.cast(image, tf.float32) / 255.0  # 归一化到[0,1]范围\n",
    "    return image, label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_label_ds = ds.map(load_and_preprocess_from_path_label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<MapDataset shapes: ((64, 64, 3), ()), types: (tf.float32, tf.int32)>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_label_ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "TensorFlow-GPU",
   "language": "python",
   "name": "tf2_gpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
